// $Id: headers.h 88 2011-10-17 18:00:11Z cedric.shih@gmail.com $
/*
 * Copyright 2011 Cedric Shih (cedric dot shih at gmail dot com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef HEADERS_H_
#define HEADERS_H_

#include <sys/tree.h>
#include <sys/queue.h>

struct niortsp_header {
	const char *name;
	const char *value;
	TAILQ_ENTRY(niortsp_header) next;
};

TAILQ_HEAD(niortsp_header_list, niortsp_header);

struct niortsp_headers *niortsp_headers_new(void);

void niortsp_headers_free(struct niortsp_headers *headers);

void niortsp_headers_clear(struct niortsp_headers *headers);

struct niortsp_header_list *niortsp_headers_list(
		struct niortsp_headers *headers);

struct niortsp_headers *niortsp_headers_clone(struct niortsp_headers *source);

int niortsp_headers_load(struct niortsp_headers *headers,
		struct niortsp_headers *source);

int niortsp_headers_set(struct niortsp_headers *headers,
		const char *name, const char *value);

int niortsp_headers_nset(struct niortsp_headers *headers,
		const char *name, size_t namelen,
		const char *value, size_t valuelen);

const char *niortsp_headers_get(struct niortsp_headers *headers,
		const char *name);

int niortsp_headers_remove(struct niortsp_headers *headers,
		const char *name);

size_t niortsp_headers_size(struct niortsp_headers *headers);

/**
 * @brief HTTP header entry.
 */
struct niortsp_headers_entry {
	char *name; /**< Parameter name. */
	char *value; /**< Parameter value. */
	struct niortsp_header node; /**< List node. */
	RB_ENTRY(niortsp_headers_entry) next;
};

RB_HEAD(niortsp_headers_tree, niortsp_headers_entry);

/**
 * @brief HTTP header.
 */
struct niortsp_headers {;
	struct niortsp_header_list list;
	struct niortsp_headers_tree tree;
	size_t size;
};

int niortsp_headers_entry_compare(struct niortsp_headers_entry *a,
		struct niortsp_headers_entry *b);

RB_PROTOTYPE(niortsp_headers_tree, niortsp_headers_entry, next,
		niortsp_headers_entry_compare);

struct niortsp_headers_entry *niortsp_headers_find(
		struct niortsp_headers *headers, const char *name);

void niortsp_headers_remove_do(struct niortsp_headers *headers,
		struct niortsp_headers_entry *entry);

#endif /*HEADERS_H_*/
